﻿using CodeTemplate.CodeSql;
using iBing.Common;
using iBing.Common.DbHelper;
using iBing.PluginFramework;
using iBing.PluginFramework.Docking;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeTemplate
{
    public partial class FrmMain : Form, IApplication
    {
        public ServiceContainer serviceContainer = new ServiceContainer();
        public DockBarServiceImp dockService;
        public PluginServiceImp pluginService;
        private Dictionary<string, string> statusList = new Dictionary<string, string>();
        private DockPanel mainPanel = null;
        private long workSpace = 0;

        public long WorkSpace
        {
            get { return workSpace; }
            set { workSpace = value; }
        }

        public FrmMain()
        {
            #region 添加Dock控件
            mainPanel = new DockPanel();
            mainPanel.DefaultFloatWindowSize = new System.Drawing.Size(240, 300);
            mainPanel.DockLeftPortion = 0.25;
            mainPanel.DockRightPortion = 0.25;
            mainPanel.Location = new System.Drawing.Point(0, 29);
            mainPanel.Name = "dockPanel1";
            mainPanel.Size = new System.Drawing.Size(819, 307);
            mainPanel.TabIndex = 5;
            this.Controls.Add(mainPanel);
            InitializeComponent();
            mainPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            #endregion

            //注册服务
            dockService = new DockBarServiceImp(this);
            pluginService = new PluginServiceImp(this);
            serviceContainer.AddService(typeof(IDockBarService), dockService);
            serviceContainer.AddService(typeof(IPluginService), pluginService);

            //数据库管理
            DbManager dbManager= new DbManager(this);
            dockService.AddDockBar("SYS_RES", "SYS_RES", dbManager, DockState.DockLeft);
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.pluginService.UnLoadAllPlugin();
        }

        #region IApplication 成员
        public DockPanel MainPanel
        {
            get
            {
                return this.mainPanel;
            }
        }

        public IDockBarService DockBarService
        {
            get
            {
                return this.dockService;
            }
        }

        public IPluginService PluginService
        {
            get
            {
                return this.pluginService;
            }
        }
        #endregion

        #region IServiceContainer 成员

        public void AddService(Type serviceType, System.ComponentModel.Design.ServiceCreatorCallback callback, bool promote)
        {
            serviceContainer.AddService(serviceType, callback, promote);
        }

        public void AddService(Type serviceType, System.ComponentModel.Design.ServiceCreatorCallback callback)
        {
            serviceContainer.AddService(serviceType, callback);
        }

        public void AddService(Type serviceType, object serviceInstance, bool promote)
        {
            serviceContainer.AddService(serviceType, serviceInstance, promote);
        }

        public void AddService(Type serviceType, object serviceInstance)
        {
            serviceContainer.AddService(serviceType, serviceInstance);
        }

        public void RemoveService(Type serviceType, bool promote)
        {
            serviceContainer.RemoveService(serviceType, promote);
        }

        public void RemoveService(Type serviceType)
        {
            serviceContainer.RemoveService(serviceType);
        }

        //由于Form类型本身间接的继承了IServiceProvider接口，所以我们要覆盖掉Form本身的实现
        //所以我们使用了new关键字
        public new object GetService(Type serviceType)
        {
            return serviceContainer.GetService(serviceType);
        }

        #endregion


        

        public void EditCreator(string dbType,string dbUrl,string schemaName, string tableName)
        {
            IDbHelper myDbHelper = null;
            switch (dbType.ToUpper())
            {
                case "MYSQL":
                    myDbHelper = new DbHelperForMySql(dbUrl);
                    break;
                case "ORACLE":
                    myDbHelper = new DbHelperForOracle(dbUrl);
                    break;
            }
            CodeForm codeForm = new CodeForm(this, myDbHelper, schemaName, tableName);
            codeForm.TabText = schemaName+"."+tableName;
            dockService.AddDockBar("TABLE_" + schemaName + "_" + tableName, "TABLE_" + schemaName + "_" + tableName, codeForm, DockState.Document);
        }

        private void toolStripButton_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = sender as ToolStripButton;
            if (btn != null)
            {
                if (btn.Text == "按SQL生成")
                {
                    using (FrmSetSql frm = new FrmSetSql())
                    {
                        if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                        {
                            IDbHelper dbHelper = frm.dbHelper;
                            string sql = frm.Sql;
                            CodeForm codeForm = new CodeForm(this, dbHelper, sql);
                            codeForm.TabText = "SQL";
                            dockService.AddDockBar("SQL", "SQL", codeForm, DockState.Document);
                        }
                    }
                }
            }
        }
    }
}